<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Repositories\Shop\WxShopCoupon;
use App\Models\Shop\WxShop;
use App\Models\Shop\WxShopGoods;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxShopCouponController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxShopCoupon(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type');
            $grid->column('num');
            $grid->column('min_purchase');
            $grid->column('goods');
            $grid->column('classifies');
            $grid->column('shops');
            $grid->column('users');
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopCoupon(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('num');
            $show->field('min_purchase');
            $show->field('goods');
            $show->field('classifies');
            $show->field('shops');
            $show->field('users');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $app_coin_name = Settings::get('app_coin_name', '硬币');
        return Form::make(new WxShopCoupon(), function (Form $form) use ($app_coin_name){
            global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
            $modle = $form->model();

            $form->display('id');
            $form->radio('type')->options([1=>'固定金额', 2=>'比例'])->default(1)->required();
            $form->decimal('num')->required();
            $form->decimal('min_purchase')->default(0);

            $form->multipleSelect('goods', '商品')->options(function ($ids) {
                if($ids){
                    $res = [];
                    foreach ($ids as $id){
                        $good = WxShopGoods::find($id);
                        if ($good) {
                            $res[$good->id] = $good->name;
                        }
                    }
                    return $res;
                }
            })->ajax('select/shop/goods')->saveAsJson();

            $options = AdminUtils::get_admin_user_shop_classifys();
            if(_empty_($options)){
                $form->html(function () use ($modle, $app_coin_name, $__MINI_GLOBAL_IS_ADMIN_SUPER__){
                    if($__MINI_GLOBAL_IS_ADMIN_SUPER__){
                        return '<div style="background-color: transparent;color: #414750;padding: 10px;border-radius: 5px;color: red;">!!! 当前没有第三层级的商品分类，无法新增商品</div>';
                    }else{
                        return '<div style="background-color: transparent;color: #414750;padding: 10px;border-radius: 5px;color: red;">!!! 当前后台账号无绑定的店铺id，无法新增商品</div>';
                    }
                }, '');
            }else{
                $form->multipleSelect('classifies','商品分类')->options($options)->saveAsJson();
            }

            $form->multipleSelect('shops')->options(function ($ids) {
                if($ids){
                    $res = [];
                    foreach ($ids as $id){
                        $shop = WxShop::find($id);
                        if ($shop) {
                            $res[$shop->id] = $shop->shop_name;
                        }
                    }
                    return $res;
                }
            })->ajax('select/shops')->saveAsJson();

            $form->multipleSelect('users')->options(function ($ids) {
                if($ids){
                    $res = [];
                    foreach ($ids as $id){
                        $user = \App\Models\User\WxUser::find($id);
                        if ($user) {
                            $res[$user->id] = $user->user_name;
                        }
                    }
                    return $res;
                }
            })->ajax('select/users')->saveAsJson();



            $form->datetime('start_time')->required();
            $form->datetime('end_time')->required();

            $form->switch('status')->options([0=>'未激活', 1=>'已激活'])->default(1)->required();

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
